package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.dto.MerchantSignDTO;
import com.ayf.payment.game.api.dto.criteria.MerchantSignCriteria;
import com.ayf.payment.game.api.entity.MerchantSign;
import com.ayf.payment.game.api.eqb.comm.SignDTO;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.MerchantSignMapper;
import com.ayf.payment.game.api.service.MerchantSignService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MerchantSignServiceImpl implements MerchantSignService {

    @Resource
    private MerchantSignMapper merchantSignMapper;

    @Override
    public MerchantSign getMerSignInfo(Integer merchantId) {
        MerchantSign merchantSign = merchantSignMapper.selectByPrimaryKey(merchantId);
        if (merchantSign == null) {
            merchantSign = new MerchantSign();
            merchantSign.setMerchantId(merchantId);
            merchantSign.setContractStatus(0);
            merchantSignMapper.insertSelective(merchantSign);
        }
        return merchantSign;
    }

    @Override
    public void updateMerSign(MerchantSign sign) {
        if (sign.getMerchantId() == null) {
            throw new ParameterException("merchantId不能为空");
        }
        merchantSignMapper.updateByPrimaryKeySelective(sign);
    }

    @Override
    public void updateMerAuthByAccId(MerchantSign sign) {
        MerchantSign temp = new MerchantSign();
        temp.setChannelAccountId(sign.getChannelAccountId());
        MerchantSign selectOne = merchantSignMapper.selectOne(temp);
        sign.setMerchantId(selectOne.getMerchantId());
        updateMerSign(sign);
    }

    @Override
    public void updateMerAuthByFlowId(MerchantSign sign) {
        MerchantSign temp = new MerchantSign();
        temp.setChannelFlowId(sign.getChannelFlowId());
        MerchantSign selectOne = merchantSignMapper.selectOne(temp);
        sign.setMerchantId(selectOne.getMerchantId());
        updateMerSign(sign);
    }

    @Override
    public void updateMerSign2(SignDTO signDTO) {
        MerchantSign sign = new MerchantSign();
        sign.setMerchantId(signDTO.getMerchantId());
        sign.setName(signDTO.getName());
        sign.setPhone(signDTO.getPhone());
        sign.setIdCard(signDTO.getIdCardNo());
        sign.setEmail(signDTO.getEmail());
        updateMerSign(sign);
    }

    @Override
    public List<MerchantSignDTO> selectSignList(MerchantSignCriteria criteria) {
        return merchantSignMapper.selectSignList(criteria);
    }

    @Override
    public void deleteMerchantSign(Integer id) {
        merchantSignMapper.deleteByPrimaryKey(id);
    }
}

